
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "tables.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "achievements.lua" )
AddCSLuaFile( "cl_postprocess.lua" )
AddCSLuaFile( "cl_spawnmenu.lua" )
AddCSLuaFile( "cl_scoreboard.lua" )
AddCSLuaFile( "cl_hudpickup.lua" )
AddCSLuaFile( "cl_deathnotice.lua" )

include( "datastream.lua" )
include( "tables.lua" )
include( "shared.lua" )
include( "player.lua" )
include( "ply_extension.lua" )
include( "achievements.lua" )
include( "messages.lua" )
include( "utilities.lua" )

drawPlayerWeapon = true

function IsValid(entity)
	return (entity and entity:IsValid())
end

function GM:Initialize( )

	self.BaseClass:Initialize( )
	datastream.Load()
	
end

/********************************
 * This is essentially the main game
 * loop
 ********************************/
function GM:Think()
	for _, player in pairs(player.GetAll()) do 
		PlayerThink(player)

	end  
end

function GM:InitPostEntity( )

end

function GM:PlayerDisconnected(ply)
	
end

function GM:PlayerInitialSpawn( ply )

end

function GM:PlayerSpawn( ply ) 

	GAMEMODE:SetupPlayer( ply )
				
end

function GM:SetupPlayer( ply )

	ply:StripWeapons()
	ply:Give("interaction")
	
	ply:SetMaxHealth(100,true)
	ply:SetSpeed(250)

end

function GM:SetupMove(ply, mv)

	if not ply:Alive() then return end
	
	if MODE_INTERACT.IsRotating then
		local userCmd = ply:GetCurrentCommand()
		MODE_INTERACT.MouseMoveX = userCmd:GetMouseX()
		MODE_INTERACT.MouseMoveY = userCmd:GetMouseY()
		ply:SnapEyeAngles(MODE_INTERACT.RotatingCamDir)
		ply:SetWalkSpeed( 0 )
	else
		ply:SetWalkSpeed( ply.WalkSpeed )
		
		if ply:KeyDown( IN_SPEED ) and mv:GetForwardSpeed() > 20 then 
			if ply:KeyDown( IN_DUCK ) or ply:KeyDown( IN_WALK ) then
				ply:SetRunSpeed( ply.WalkSpeed  )
			else
				ply:SetRunSpeed( ply.WalkSpeed + 120 )
			end
			return
		elseif ply:KeyDown( IN_WALK ) then
			ply:SetWalkSpeed( ply.WalkSpeed )
			return
		elseif ply:KeyDown( IN_BACK ) then
			ply:SetWalkSpeed( ply.WalkSpeed )
			return
		elseif ply:KeyDown( IN_DUCK ) then
			ply:SetWalkSpeed( ply.WalkSpeed  )
			return
		else
			ply:SetWalkSpeed( ply.WalkSpeed )
			return
		end
	end
end

function GM:PlayerUse(player, entity)

end

function GM:KeyRelease( ply, key )
	if(key == IN_USE) then

		local rayTrace = ply:GetEyeTrace()  
		local traceEntity = rayTrace.Entity

		local showUIData = {};
		if IsValid(MODE_INTERACT.HoldingObject) then

			showUIData.traceType = "PhysicsEntity";
			showUIData.entIndex = MODE_INTERACT.HoldingObject:EntIndex();

		elseif IsPhysiscsEntity(traceEntity) then
		
			showUIData.traceType = "PhysicsEntity";
			showUIData.entIndex = traceEntity:EntIndex();
						
		elseif IsCameraNode(traceEntity) then

			if traceEntity:GetClass() == "animated_camera" then
				showUIData.traceType = "AnimatedCameraNode";
			else
				showUIData.traceType = "StaticCameraNode";
			end
			showUIData.entIndex = traceEntity:EntIndex();

		elseif traceEntity:GetClass() == "actor_sequence" then
			showUIData.traceType = "ActorSequenceNode";
			showUIData.entIndex = traceEntity:EntIndex();

		else

			showUIData.traceType = "Default";

		end
			
		datastream.Send(ply,"ShowUIPhysEnt", showUIData, nil);
				
	end
end

function GM:ShowHelp(ply)
	umsg.Start("ShowHelp", ply)
	umsg.End()
end

function GM:ShowSpare1(ply)
	
end

function GM:ShowSpare2(ply)

end

function GM:ShowTeam(ply)
	
end

function GM:PlayerDeathSound()
	return false
end

function GM:EntityTakeDamage( ent, inflictor, attacker, amount, dmginfo )
	
end

function GM:ScalePlayerDamage( ply, group, dmginfo )
	dmginfo:SetDamage(0)
return dmginfo 
end

function GM:ScaleNPCDamage( npc, group, dmginfo )
	dmginfo:SetDamage(0)
return dmginfo 
end

function GM:PlayerDeathThink( ply )
	ply:Spawn()
end

function GM:DoPlayerDeath(ply, attacker, dmginfo)
	ply:Spawn()
end

function GM:CanPlayerSuicide( ply )
	return false
end 	

function GM:PlayerTraceAttack( ply, dmginfo, dir, trace )
	return self.BaseClass:PlayerTraceAttack(ply, dmginfo, dir, trace)
end	

function SetPlayerModel(ply,cmd,args)
	ply:ConCommand("cl_playermodel "..args[1])
	ply:Notice("Your model will update when you respawn",Color(0,0,0,255),3)
end
concommand.Add("setplayermodel", SetPlayerModel)

local list = file.Find("../gamemodes/*")
for k,v in pairs(list) do
	for c,d in pairs(file.FindInLua("../gamemodes/"..v.."/gamemode/register.lua")) do
		if file.Exists("../gamemodes/"..v.."/gamemode/register.lua") and SERVER then
			include(v.."/gamemode/register.lua")
		end
	end
end

function DebugMsg(txt)
	for k,v in pairs(player.GetAll()) do
		v:ChatPrint("DEBUG: "..tostring(txt))
	end
return txt end	